/**
 * 
 */
package ua.at.tsvetkov.samizdat.adapters;

import ua.at.tsvetkov.application.AppConfig;
import ua.at.tsvetkov.samizdat.Key;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.data.BookColor;
import ua.at.tsvetkov.samizdat.data.BookDownloader;
import ua.at.tsvetkov.util.Log;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * D@author A.Tsvetkov 2011, http://tsvetkov.at.ua, al@ukr.net
 */
public class AdtAcBook extends BaseAdapter {

	private LayoutInflater	mInflater;
	private Activity			context;
	private BookDownloader	bookDownloader;
	private float				textSize = 16;
	private int	size;
	private boolean	needToChange;
	private BookColor	bookColor;

	public AdtAcBook(Activity activity, BookDownloader bookDownloader) {
		this.context = activity;
		this.mInflater = LayoutInflater.from(context);
		this.bookDownloader = bookDownloader;
		this.size = 0;
		this.bookColor = BookColor.colors[AppConfig.getInt(Key.BOOK_COLORS_SET, 0)];
		this.needToChange = true;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
		needToChange = true;
		update();
	}

	public void setBookColor(BookColor bookColor) {
		this.bookColor = bookColor;
		needToChange = true;
		update();
	}
	/**
	 * @param listView
	 * @param currentStart the currentStart to set
	 */
	public void update() {
		this.size = bookDownloader.getLineCount();
		context.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				AdtAcBook.this.notifyDataSetChanged();
			}
		});

	}
	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return size;
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int key) {
		return bookDownloader.getString(key);
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	/* (non-Javadoc)
	 * @see android.widget.BaseAdapter#areAllItemsEnabled()
	 */
	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see android.widget.BaseAdapter#isEnabled(int)
	 */
	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextHolder holder = new TextHolder();
		if (convertView == null || convertView.getTag() != holder || needToChange) {
			convertView = mInflater.inflate(R.layout.adt_ac_book, null);
			holder.text = (TextView) convertView.findViewById(R.id.AdtAcBookTextView);
			convertView.setBackgroundResource(bookColor.background);
			holder.text.setTextColor(bookColor.textColor);
			holder.text.setTextSize(textSize);
			convertView.setTag(holder);
			needToChange = false;
		} else {
			holder = (TextHolder) convertView.getTag();
		}
		try {
			holder.text.setText(bookDownloader.getString(position));
		} catch (Exception e) {
			Log.w("Line=" + position, e);
		}
		return convertView;
	}

	class TextHolder {
		TextView	text;
	}

}
